//
//  KFLRemoveNotificationRequest.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/1/30.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//

#import "KFLRemoveNotificationRequest.h"
#import "Model_notificationProblem.h"
#import "Model_notificationSystem.h"

@implementation KFLRemoveNotificationRequest
@synthesize string_type;

- (void)startToRequestWithBlock:(requestResultBlock)resultBlock;
{
    self.kflEngine.hostName = @"deleteallydinfo";

    NSString *string_uid = [[Model_user sharedInstance] getUserID];
    NSString *string_token = [[Model_user sharedInstance] getToken];
    
    NSMutableArray *array_sort = [NSMutableArray arrayWithObjects:@"deleteallydinfo",string_uid,string_type,string_token,Model,Version,nil];
    NSString *string_sign = [self getSignWithArray:array_sort];
    
    NSString *string_url = [KWNBaseURL stringByAppendingString:[NSString stringWithFormat:@"lawyer/deleteallydinfo?method=deleteallydinfo&id=%@&type=%@&token=%@&src=%@&edition=%@&sig=%@",string_uid,string_type,string_token,Model,Version,string_sign]];
    
//    __weak KFLRemoveNotificationRequest *aself = self;
    MKNetworkOperation* op = [self.kflEngine operationWithURLString:[self urlStrUTF8:string_url]];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            
            NSDictionary *kindDic = [NSJSONSerialization JSONObjectWithData:completedOperation.responseData options:NSJSONReadingMutableLeaves error:&error];
            
            NSNumber *number_code = [kindDic objectForKey:@"code"];
            NSString *string_message = [kindDic objectForKey:@"msg"];
            NSMutableArray *array_result = [NSMutableArray array];
            
            if(!number_code || !string_message){
                [array_result addObject:@"系统发生错误"];
            }
            else{
                if (number_code.integerValue == 10000) {
                    if ([string_type integerValue] == 0) {
                        NSArray *arr = [kindDic objectForKey:@"data"];
                        if (!arr && [arr isKindOfClass:[NSNull class]]) {
                            arr = [[NSArray alloc] init];
                        }
                        array_result = [self returnProblemDataArr:arr];
                    }
                    else
                    {
                        NSArray *arr = [kindDic objectForKey:@"data"];
                        if (!arr && [arr isKindOfClass:[NSNull class]]) {
                            arr = [[NSArray alloc] init];
                        }
                        array_result = [self returnSystemDataArr:arr];
                    }
                }
                else{
                    [array_result addObject:string_message];
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock(number_code.integerValue,array_result);
            });
        });
    }errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {
        NSMutableArray *array_data = [NSMutableArray arrayWithObject:@"获取失败"];
        resultBlock(10001,array_data);
    }];
    [self.kflEngine enqueueOperation:op];
}

- (NSMutableArray *)returnProblemDataArr:(NSArray *)arr
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i < arr.count; i++) {
        NSDictionary *dic = [arr objectAtIndex:i];
        NSString *flag_ = [dic objectForKey:@"flag"];
        if ([flag_ integerValue] == 0) {
            Model_notificationProblem *notProblem = [[Model_notificationProblem alloc] init];
            [notProblem getProblemDataWithDic:dic];
            [array addObject:notProblem];
        }
        else if([flag_ integerValue] == 1)
        {
            Model_notificationProblemComments *notProblemComment = [[Model_notificationProblemComments alloc] init];
            [notProblemComment getProblemCommentsDataWithDic:dic];
            [array addObject:notProblemComment];
        }
    }
    return array;
}

- (NSMutableArray *)returnSystemDataArr:(NSArray *)arr
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i < arr.count; i++) {
        NSDictionary *dic = [arr objectAtIndex:i];
        Model_notificationSystem *system = [[Model_notificationSystem alloc] init];
        [system getSystemDataWithDic:dic];
        [array addObject:system];
    }
    return array;
}

@end
